
package com.feib.soeasy.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.PaymentConfigDao;
import com.feib.soeasy.model.PaymentConfig;
import com.feib.soeasy.pagination.PaginatedList;


/**
 * @title (#)PaymentConfigHibernateDao.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/3/7 create by Jimmy Liu<br>
 */
@Repository("paymentConfigDao")
public class PaymentConfigHibernateDao extends BaseHibernateDao<PaymentConfig, Long> implements PaymentConfigDao {

    /* (non-Javadoc)
     * @see com.feib.soeasy.dao.PaymentConfigDao#queryByPaymentConfig(com.feib.soeasy.model.PaymentConfig, com.feib.soeasy.pagination.PaginatedList)
     */
    @SuppressWarnings("unchecked")
    @Override
    public PaginatedList<PaymentConfig> queryByPaymentConfig(PaymentConfig paymentConfig, PaginatedList<PaymentConfig> paginationConfig) {
        PaginatedList<PaymentConfig> result = new PaginatedList<PaymentConfig>();

        result.setPageSize(paginationConfig.getPageSize());
        result.setPageNumber(paginationConfig.getPageNumber());
        result.setSortCriterion(paginationConfig.getSortCriterion());
        result.setSortDirection(paginationConfig.getSortDirection());

        result.setFullListSize(super.countByDetachedCriteria(
                        createDetachedCriteriaByPaymentConfig(paymentConfig)));


        // 筆數大於0
        if (0 < result.getFullListSize())
        {
            DetachedCriteria criteria = 
                createDetachedCriteriaByPaymentConfig(paymentConfig);

            //排序
            if (null != paginationConfig.getSortCriterion())
            {
                // 由小到大
                if (PaginatedList.SORT_DIRECTION_ASC == paginationConfig.getSortDirection())
                {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
                else // 由大到小
                {
                    criteria.addOrder(Order.asc(paginationConfig.getSortCriterion()));
                }
            }


            int maxResults = result.getPageSize(); //the maximum number of results
            int firstResult = result.getFirstIndexForCurrentPage();  //the first result to retrieve, numbered from 0
            result.setPaginatedList(this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults));
        }

        return result;
    }

    /**
     * @param paymentConfig
     * @return
     */
    private DetachedCriteria createDetachedCriteriaByPaymentConfig(PaymentConfig entity) {
        
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        
        Example example = Example.create(entity);
        example.enableLike(MatchMode.START);
        example.excludeZeroes();
        criteria.add(example);

        // 屬於哪一個群組
        if (null != entity.getGroup())
        {
            DetachedCriteria detachedCriteria = criteria.createCriteria("group");

            // PK 有值，需加入為查詢條件
            if (null != entity.getGroup().getGroupPk())
            {
                detachedCriteria.add(Restrictions.idEq(entity.getGroup().getGroupPk()));
            }
            else
            {
                // Group 其他查詢條件
                Example e = Example.create(entity.getGroup());
                e.enableLike(MatchMode.START);
                e.excludeZeroes();
                detachedCriteria.add(e);
            }

        }
        
        if (StringUtils.isNotEmpty(entity.getConfigNo()))
            criteria.add(Restrictions.eq("configNo", entity.getConfigNo()));
        if (StringUtils.isNotEmpty(entity.getConfigName()))
            criteria.add(Restrictions.like("configName", entity.getConfigName(), MatchMode.START));
        
        return criteria;
    }
    
    @SuppressWarnings("unchecked")
    public List<PaymentConfig> queryForListByPaymentConfig(PaymentConfig paymentConfig)
    {
        DetachedCriteria criteria = 
            createDetachedCriteriaByPaymentConfig(paymentConfig);
        return this.getHibernateTemplate().findByCriteria(criteria);
    }

    /* 
     * (non-Javadoc)
     * @see com.feib.soeasy.dao.PaymentConfigDao#findByConfigNo(java.lang.String, java.lang.Long)
     */
    @Override
    public PaymentConfig findByConfigNo(String configNo, Long groupPk) {
        if( StringUtils.isEmpty(configNo))
            return null;
        
        DetachedCriteria criteria = DetachedCriteria.forClass(this.getPersistentClass());
        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);

        criteria.add(Restrictions.eq("configNo", configNo));
        
        DetachedCriteria detachedCriteria = criteria.createCriteria("group");
        detachedCriteria.add(Restrictions.idEq(groupPk));
        

        @SuppressWarnings("unchecked")
        List<PaymentConfig> groups = this.getHibernateTemplate().findByCriteria(criteria);

        return DataAccessUtils.singleResult(groups);
    }

    

}
